<%--
Created by IntelliJ IDEA.
User: 陪你度过漫长岁月
Date: 2022/8/5
Time: 11:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>轮播图列表</title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>
    <style type="text/css">
        .layui-form-checkbox[lay-skin=primary] i {
            right: auto;
            left: 10;
            top: 25;
            width: 16px;
            height: 16px;
            line-height: 16px;
            border: 1px solid #d2d2d2;
            font-size: 12px;
            border-radius: 2px;
            background-color: #fff;
            -webkit-transition: .1s linear;
            transition: .1s linear;
        }
        .layui-table img{
            max-width: 250px;
        }
        img{
            width: 250px;
            height: 150px;
        }
    </style>


</head>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="role-table" class="layui-table" lay-filter="role-table"></table>
    </div>
</div>

<script type="text/html" id="role-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" id="btn1">
        切换-主页
    </button>
</script>

<script type="text/html" id="role-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power"><i
            class="layui-icon layui-icon-vercode"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="role-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="role-enable" {{
           d.state==1 ? 'checked' : '' }} />
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<div align="center">
    <%--    layui 禁用按钮属性 layui-btn-disabled--%>
</div>

<script type="text/html" id="imgtmp">
    <img onclick="img('{{d.pic}}')" class="img" src="{{d.pic}}" width="250px" height="150px">
</script>
<script type="text/html" id="updateTime">
    {{d.updateTime==null ? '无' : 'd.updateTime'}}
</script>
<script type="text/html" id="produceName">
    {{d.produceName==null ? '无' : 'd.produceName'}}
</script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "operate/";

        let cols = [
            [
                {type: 'checkbox', title: '选择', width: 80, sort: false}
                , {field: 'id', title: '编号', width: 80, sort: false}
                , {field: 'location', title: '位置', width: 100, sort: false}
                , {field: 'produceName', title: '商品名称', templet: '#produceName', width: 100, sort: false}
                , {field: 'pic', title: '图片', templet: '#imgtmp', style: "height:150px", width: 280, sort: false}
                , {field: 'creationTime', title: '创建时间', width: 160, sort: false}
                , {field: 'updateTime', title: '修改时间', templet: '#updateTime', width: 100, sort: false}
                , {field: 'state', title: '状态', templet: '#role-enable', width: 100, sort: false}
                , {field: 'sort', title: '排序', width: 100, sort: false}
                , {field: 'name', title: '名字', width: 100, sort: false}
            ]
        ]

        table.render({
            elem: '#role-table',
            url: '${pageContext.request.contextPath}/ShowMainListServlet',
            page: true,
            cols: cols,
            skin: 'line',
            async: true,
            headers: {
                token: localStorage.token
            },
            request: {
                pageName: 'pageNo',
                limitName: 'pageSize',
            },
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        $(document).on('click','#btn1',function () {
            var lon = $('#btn1').html().trim();
            table.render({
                elem: '#role-table',
                url: '${pageContext.request.contextPath}/ShowMainListServlet?location='+lon.split("-")[1],
                page: true,
                cols: cols,
                skin: 'line',
                async: true,
                headers: {
                    token: localStorage.token
                },
                request: {
                    pageName: 'pageNo',
                    limitName: 'pageSize',
                },
                toolbar: '#role-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });
            if (lon == "切换-主页") {
                lon = "切换-首页";
            }else {
                lon = "切换-主页"
            }
            $('#btn1').text(lon);
        })

        $('.btn1').on('click',function () {
            table.render({
                elem: '#role-table',
                url: '${pageContext.request.contextPath}/ShowMainListServlet?location=${sessionScope.s=="首页" ? "主页" : "首页"}',
                page: true,
                cols: cols,
                skin: 'line',
                async: true,
                headers: {
                    token: localStorage.token
                },
                request: {
                    pageName: 'pageNo',
                    limitName: 'pageSize',
                },
                toolbar: '#role-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });
            return false;
        })

        table.on('tool(role-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'power') {
                window.power(obj);
            }
        });

        table.on('toolbar(role-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(role-query)', function (data) {
            table.reload('role-table', {where: data.field})
            return false;
        });

        form.on('switch(role-enable)', function (obj) {
            var state = 0;
            if (obj.elem.checked==true){
                state = 1;
            }else{
                state = 0;
            }
            var id = this.value;
            $.getJSON('${pageContext.request.contextPath}/ShowMainStateServlet',{id:id,state:state},function (data) { })
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: "新增",
                closeBtn: 1,
                area: [common.isModile() ? '100%' : '400px', common.isModile() ? '100%' : '400px'], scrollbar: false,
                skin: 'layui-layer-rim my-layer1',
                shadeClose: false,
                content: MODULE_PATH + 'carouselAdd.jsp',
                offset: ['100px','250px'],
                end: function () {
                }
            });
        }

        window.power = function (obj) {
            layer.open({
                type: 2,
                title: '授权',
                shade: 0.1,
                area: ['320px', '400px'],
                content: MODULE_PATH + 'edit.jsp'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '400px'],
                content: MODULE_PATH + 'edit.jsp'
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该角色', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['roleId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确认是否删除？', {
                icon: 3,
                title: '提示',
                offset: ['200px','420px']
            }, function (index) {
                layer.close(index);
                $.getJSON('${pageContext.request.contextPath}/ShowMainDelServlet',{ids:ids},
                    function (data) {
                        if (data) {
                            var lon = $('#btn1').html().trim();
                            if (lon == "切换-主页") {
                                lon = "切换-首页";
                            }else {
                                lon = "切换-主页"
                            }
                            table.render({
                                elem: '#role-table',
                                url: '${pageContext.request.contextPath}/ShowMainListServlet?location='+lon.split("-")[1],
                                page: true,
                                cols: cols,
                                skin: 'line',
                                async: true,
                                headers: {
                                    token: localStorage.token
                                },
                                request: {
                                    pageName: 'pageNo',
                                    limitName: 'pageSize',
                                },
                                toolbar: '#role-toolbar',
                                defaultToolbar: [{
                                    title: '刷新',
                                    layEvent: 'refresh',
                                    icon: 'layui-icon-refresh',
                                }, 'filter', 'print', 'exports']
                            });
                        }
                    }
                )
            });
        }

        window.refresh = function () {
            table.reload('role-table');
        }
    })
</script>
<script>
    function img(pic) {
        layui.use(['table','form','jquery','common'],function () {
            let common = layui.common;
            let MODULE_PATH = "operate/";

            layer.open({
                type: 2,
                title: "",
                closeBtn: 1,
                area: [common.isModile() ? '100%' : '700px', common.isModile() ? '100%' : '450px'], scrollbar: true,
                skin: 'layui-layer-rim my-layer1',
                shadeClose: false,
                content: MODULE_PATH + 'image.jsp?pic='+pic,
                offset: ['50px','180px'],
                end: function () {
                }
            });
        })
    }
</script>
</body>
</html>
